<template>
	<view class="view_page">
		<view class="view_content">
			<me-section :title="pageName" type="line">
				<view class="module_item">
					<me-form v-if="formCols.length" ref="meForms" v-model="params" :props="formProps" :columns="formCols" @submit="goSubmit"/>
				</view>
			</me-section>
		</view>
	</view>
</template>

<script>
import meSection from '../../components/ui/me-section/index.vue';
import meForm from '../../components/ui/me-form/index.vue';
import * as Apis from '@/api/index'
import { getConfig } from './config';
export default {
	data() {
		return {
			pageName:'',
			params:{},
			tabList:['单据信息','入库明细'],
			formProps:{},
			formCols:[],
			storeList:[]
		};
	},
	components:{
		meSection,
		meForm
	},
	onLoad(options) {
		const configInfo = getConfig()
		this.formProps = configInfo.formProps
		this.formCols = configInfo.formCols
		this.params.recordType = Number(options.type || 1)
		this.pageName = options.pageName
		this.getStoreList()
		this.$nextTick(()=>{
			uni.setNavigationBarTitle({
				title: this.$t('common.newAdd')
			});
		})
  },
	methods: {
		async getStoreList(){
			await this.$nextTick()
			try {
				const fData = {
					record_type:this.params.recordType
				}
				const {success,data,message} = await Apis['getSelectStore'](fData)
				if (!success) {
					this.$modal.showToast(message);
				}
				const list = (data || []).map((item)=>{
					item.value = item.id
					item.text = item.store_name
					return item
				})
				this.storeList = list
				const listLabels =  (data || []).map((item)=>item.store_name)

				this.$nextTick(() => {
					console.log('list',list,this)
					const itemInfo = this.$refs['meForms'].getItem('out_store_id')
					if(itemInfo && itemInfo.props){
						itemInfo.props.localdata = list
						itemInfo.props.candidates = listLabels
					}
					const itemInfo1 = this.$refs['meForms'].getItem('enter_store_id')
					if(itemInfo1 && itemInfo1.props){
						itemInfo1.props.localdata = list
						itemInfo1.props.candidates = listLabels
					}
				})
			} catch (error) {
				console.log('error',error)
			}
		},
		async goSubmit(){
			try {
				const configInfo = getConfig()
				const apiName = configInfo['apiName'][this.params.recordType]
				if(!apiName){
					return uni.showToast({ title: this.$t('common.develop') })
				}
				const fData = Object.assign({},this.params)
				if(fData.out_store_id){
					const outItem = this.storeList.find((item)=> item.store_name === fData.out_store_id)
					if(!outItem){
						return this.$modal.showToast(this.$t('common.noFind')+this.$t('erp.outStore'));
					}
					fData.out_store_id = outItem.id
				}
				if(fData.enter_store_id){
					const enterItem = this.storeList.find((item)=> item.store_name === fData.enter_store_id)
					if(!enterItem){
						return this.$modal.showToast(this.$t('common.noFind')+this.$t('erp.enterStore'));
					}
					fData.enter_store_id = enterItem.id
				}
				const {success,data,message} = await Apis[apiName](fData)
				if (!success) {
					this.$modal.showToast(message || (this.$t('common.submit')+this.$t('common.success')));
				}
				this.$modal.showToast(message || (this.$t('common.submit')+this.$t('common.fail')));
				this.goBack('refresh')
			} catch (error) {
				console.log('error',error)
			}
		},
		goBack(type = undefined){
			if(type === 'refresh'){
				this.$tab.redirectTo(`/pages/record/list?type=${this.params.recordType}&pageName=${this.pageName}`);
			}else{
				this.$tab.navigateBack({ data: 1 })
			}
		},
		async goSearch (){
			console.log('search',this.params)
		}
	}
};
</script>

<style lang="scss">
.list_empty {
	position: relative;
	top: -50%;
	padding-top: 154rpx;
}
.view_page {
	width: 100%;
	height: auto;
	padding: 30rpx;
	.view_top{
		height: 220rpx;
	}
	.view_top_fixed{
		position: fixed;
		left: 0;
		top: 88rpx;
		z-index: 10;
		width: 100%;
		height: auto;
		background: #fff;
    box-shadow: 0px 2rpx 10rpx rgba(193, 193, 193, 0.2);
		.view_search{
			width: 100%;
			height: 110rpx;
			border-top: 1rpx solid #dcdcdc;
			border-bottom: 1rpx solid #dcdcdc;
			display: flex;
			align-items: center;
			justify-content: space-between;
			/deep/ .uni-searchbar{
				flex: 1;
			}
		}
		.view_tabs{
			width: 100%;
			height: 110rpx;
			padding: 15rpx 20rpx 15rpx;
			/deep/ .segmented-control--button{
				.segmented-control__item{
					border-radius: 50rpx;
					border-color: transparent !important;
					.segmented-control__text{
						color: #333 !important;
					}
					&.segmented-control__item--button--active{
						border:2rpx solid rgba(41, 121, 255, 1) !important;
						background-color: rgba(41, 121, 255, 0.3) !important;
						.segmented-control__text{
							color: rgba(41, 121, 255, 1) !important;
						}
					}
				}
			}
		}
	}
	.view_content{
		.module_item{
			padding: 30rpx;
		}

	}
	/deep/ .list_content{
		padding: 30rpx;
		padding-top: calc(30rpx + 0rpx);
		background: transparent;
		.list_item{
			border-radius: 30rpx;
			background: #fff;
			overflow: hidden;
			&+.list_item{
				margin-top: 20rpx;
			}
		}

	}
}
</style>
